package com.demo.study.thread;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.concurrent.Callable;

/***
 * 多线程实现求和
 *
 */
public class MyCallable implements Callable<List<String>> {
    private int taskNum;    //任务编号
    private int number;     //求和数值
 
    MyCallable(int taskNum, int number) {
        this.taskNum = taskNum;
        this.number = number;
    }
 
    @Override
    public List<String> call() {
        System.out.println(">>>" + taskNum + " 任务启动");
        Date dateTmp1 = new Date();
 
        //执行具体的任务内容
        /*long sum = 0;
        for (int x = 1; x <= number; x++) {
            sum += x;
        }*/
       String sum =   game(1);
 
        //任务耗时
        Date dateTmp2 = new Date();
        long time = dateTmp2.getTime() - dateTmp1.getTime();
 
        System.out.println(">>>" + taskNum + " 任务结束");
 
        List<String> integerList = new ArrayList<>();
        integerList.add(String.valueOf(time));
        integerList.add(String.valueOf(sum));
        return integerList;
    }


    public static String game(int count){
        StringBuffer sb = new StringBuffer();
        String str = "0123456789";
        Random r = new Random();
        for(int i=0;i<count;i++){
            int num = r.nextInt(str.length());
            sb.append(str.charAt(num));
            str = str.replace((str.charAt(num)+""), "");
        }
        return sb.toString();
    }
}
